// ActionScript file
package com.relman.components {
    import com.relman.util.ServerErrors;
    import mx.validators.Validator;
    import mx.validators.ValidationResult;

    public class ServerErrorValidator extends Validator {
        /**
         * These are the ServerErrors that apply specifically to this
         * ServerErrorValidator.
         */
        private var _serverErrors:ServerErrors;

        /**
         * The field of the ServerErrors we are interested in.
         */
        public var field:String;

        /**
         * The ServerErrors we are interested in.
         */
        public function set serverErrors(pServerErrors:ServerErrors):void {
            _serverErrors = pServerErrors;
            validate();
        }

        public function ServerErrorValidator() {
            field = ServerErrors.BASE;//default to being on the BASE
            _serverErrors = null;
            super();
        }

        override protected function doValidation(value:Object):Array {
            var fieldErrors:Array = _serverErrors.getErrorsForField(field);
            if (fieldErrors.length == 0) return [];
            if (fieldErrors.length == 1) return [{errorMessage:fieldErrors[0]}];
            var bullet:String = "- ";
            return [{errorMessage:bullet.concat(fieldErrors.join("\n- "))}];
        }
    }
}
